package com.lumlord.controller;

import com.lumlord.common.annotation.ControllerRequestMapping;
import com.lumlord.common.domain.ApiMappingInfo;
import com.lumlord.common.exception.BusinessException;
import com.lumlord.common.spring.ApiControllerMapping;
import com.lumlord.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import com.lumlord.common.annotation.Api;
import com.lumlord.common.base.ApiController;
import org.springframework.web.bind.annotation.RequestMethod;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ControllerRequestMapping(path = "/")
@Controller
@Api(moduleName = "运维展示入口",NameSpace = "/")
public class HomeController extends ApiController{

	@Api(actionName = "获取服务信息", method = RequestMethod.GET, path = "/appver",isAllow = true,isJsonRequest = false)
	public Object welcome() throws BusinessException {

		Map map = new HashMap<String,Object>();
		map.put("serverName",serverName);
		map.put("port",port);
		//map.put("apis",apis());
		map.put("systemTime", DateUtils.getTime());

		return  map;
	}

	public List<String> apis() {
		List<String> list = new ArrayList<String>(apiControllerMapping.getApis().size());
		for (ApiMappingInfo apiInfo : apiControllerMapping.getApis()) {

			list.add( ";Method = "+ apiInfo.getMethod().getName() + ";Clazz" + apiInfo.getClazz().toString() + ";"
				+"  API info : "	+ (apiInfo.getApi().path().length>0?" path = "+apiInfo.getApi().path()[0]+"; ":"")+" "
				+(apiInfo.getApi().method().length>0?" method ="+apiInfo.getApi().method()[0].name():"")+" ; actionName "+	apiInfo.getApi().actionName()
			);
		}
	return list;
	}


	@Autowired
	private ApiControllerMapping apiControllerMapping;
	@Value("${server.servlet.context-path}")
	private String serverName;
	@Value("${server.port}")
	private String port;


}
